package com.zhiwei.fast.views

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.annotation.Px
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SizeUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import java.util.concurrent.TimeUnit

/**
 * Create by zhiweizhu on 2022/2/26
 */
class AutoScrollRecyclerView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : RecyclerView(context, attrs) {




    private var mAutoTask: Disposable? = null

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        return true
    }

    fun start(perid: Long = 50, @Px scrollBy: Int = SizeUtils.dp2px(100f), isUpOrDown: Boolean = true) {
        if (mAutoTask != null && !mAutoTask!!.isDisposed) {
            mAutoTask!!.dispose()
        }
        mAutoTask = Observable.interval(100, perid, TimeUnit.MILLISECONDS).observeOn(
            AndroidSchedulers.mainThread()).subscribe { smoothScrollBy(0, scrollBy) }
    }

    fun stop() {
        if (mAutoTask != null && !mAutoTask!!.isDisposed) {
            mAutoTask!!.dispose()
            mAutoTask = null
        }
    }

}